package com.wl156.adapters;

import org.ksoap2.serialization.SoapObject;

import com.wl156.R;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OrderAdapter extends BaseAdapter implements OnClickListener {
	private static final String TAG = null;
	private LayoutInflater mInflater;
	private SoapObject mData;
	private Context mContext;

	public OrderAdapter(Context context, SoapObject data) {
		mInflater = LayoutInflater.from(context);
		mData = data;
		mContext = context;
	}

	@Override
	public int getCount() {
		if (mData == null) {
			return 0;
		}
		return mData.getPropertyCount();
	}

	@Override
	public SoapObject getItem(int position) {
		return (SoapObject) mData.getProperty(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.valueOf(getItem(position).getProperty(0).toString());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_order, null);
		}

		TextView from = (TextView) convertView.findViewById(R.id.from);
		TextView to = (TextView) convertView.findViewById(R.id.to);
		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView weight = (TextView) convertView.findViewById(R.id.weight);

		TextView driver = (TextView) convertView.findViewById(R.id.driver);
		TextView createDate = (TextView) convertView
				.findViewById(R.id.createDate);
		TextView state = (TextView) convertView.findViewById(R.id.state);
		TextView contact = (TextView) convertView.findViewById(R.id.contact);

		TextView phoneDial = (TextView) convertView.findViewById(R.id.dial);
		phoneDial.setOnClickListener(this);

		try {
			setTextViewStringFromProperty(position, from, 2);
			setTextViewStringFromProperty(position, to, 3);
			setTextViewStringFromProperty(position, name, 4);
			setTextViewStringFromProperty(position, weight, 5);
			setTextViewStringFromProperty(position, driver, 6);
			setTextViewStringFromProperty(position, createDate, 9);
			setTextViewStringFromProperty(position, state, 10);
			setTextViewStringFromProperty(position, contact, 11);

			setPhoneView(position, phoneDial, 7);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;
	}

	protected void setTextViewStringFromProperty(int position, TextView tv,
			int index) {
		Object prop = getItem(position).getProperty(index);
		String text = "";
		if (prop != null) {
			text = prop.toString();
		}
		if (text.equals("anyType{}")) {
			text = "";
		}
		tv.setText(text);
	}

	protected void setPhoneView(int position, TextView view, int index) {
		Object prop = getItem(position).getProperty(index);
		if (prop == null) {
			return;
		}
		String phone = prop.toString();
		if (phone.equals("anyType{}")) {
			phone = "";
			view.setVisibility(View.INVISIBLE);
		} else {
			view.setTag(phone);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dial:
			Intent i = new Intent(Intent.ACTION_CALL);
			i.setData(Uri.parse("tel:" + v.getTag().toString()));
			mContext.startActivity(i);
			break;
		}
	}
}
